﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace TouchUmlUI
{
    class TextRecognizerSizer
    {
        static double width;
        static double height;

        static double max_width;
        static double max_height;

        const double ribbon_height = 200.0; //183.0
        const double scale_factor = 3.0;
        const double max_factor = 0.45;
        const double edge_cut = 10;

        static public void Initalize()
        {
            TextRecognizerSizer.height = SignalR.windowParent.ActualHeight - TextRecognizerSizer.ribbon_height;
            TextRecognizerSizer.width = SignalR.windowParent.ActualWidth;

            TextRecognizerSizer.max_height = (TextRecognizerSizer.height * TextRecognizerSizer.max_factor);
            TextRecognizerSizer.max_width = (TextRecognizerSizer.width * TextRecognizerSizer.max_factor);
            
        }

        static public Size getSizeNotatkaObj(Size size)
        {
            Point pt = correctSize(new Point(size.Width, size.Height));
            return new Size(pt.X, pt.Y);
        }

        static public Size getSizeKlasaInterfejsObj()
        {
            Size ret = new Size();
            ret.Width = 0.35 * TextRecognizerSizer.width;
            ret.Height = 0.3 * TextRecognizerSizer.height;
            return ret;
        }

        static public Size getSizeName()
        {
            Size ret = new Size();
            ret.Width  = 0.25 * TextRecognizerSizer.width;
            ret.Height = 0.2  * TextRecognizerSizer.height;
            return ret;
        }

        public static Point[] getEllipse(Point topleft, Size size)
        {
            Point[] ret = new Point[2];

            ret[0].X = topleft.X - size.Width / 2.0f;
            ret[0].Y = topleft.Y - size.Height / 2.0f;
            ret[1].X = size.Width * 2;
            ret[1].Y = size.Height * 2;

            if (ret[1].X > 600)
            {
                ret[0].X += (ret[1].X - 600) / 2.0f;
                ret[1].X = 600;
            }

            if (ret[1].Y > 400)
            {
                ret[0].Y += (ret[1].Y - 400) / 2.0f;
                ret[1].Y = 400;
            }

            fixSize(ref ret[0], ref ret[1]);

            //ret[1] = TextRecognizerSizer.correctSize(ret[1]);

            return ret;
        }


        static private void fixSize(ref Point topleft, ref Point size)
        {
            double dif;

            if(topleft.X < edge_cut)
            {
                dif = edge_cut - topleft.X;
                topleft.X = edge_cut;
                size.X = size.X - dif;
            }

            if(topleft.Y < edge_cut)
            {
                dif = edge_cut - topleft.Y;
                topleft.Y = edge_cut;
                size.Y = size.Y - dif;
            }

            if (topleft.X + size.X > width - edge_cut)
            {
                dif = (topleft.X + size.X) - (width - edge_cut);
                topleft.X = topleft.X - dif;
                size.X = size.X - dif;
            }

            if (topleft.Y + size.Y > height - edge_cut)
            {
                dif = (topleft.Y + size.Y) - (height - edge_cut);
                topleft.Y = topleft.Y - dif;
                size.Y = size.Y - dif;
            }

        }

        static private Point correctSize(Point size)
        {
            if (size.X < 150)
                size.X = 150;
            if (size.Y < 70)
                size.Y = 70;
            if (size.X > TextRecognizerSizer.width)
                size.X = TextRecognizerSizer.width;
            if(size.Y > TextRecognizerSizer.height)
                size.Y = TextRecognizerSizer.height;
            
            return size;
        }

        

    }
}
